// (function (window) {
 function NativeBridge() {
		let that = this;
		this.indexCount = 1000;
		this.callAppMap = {};
		this.onPressBackKey = null;
		window.executeJS = function (callAppIndex, stringResult) {
			that.appCall(callAppIndex, stringResult);
		};
	}
	let u = navigator.userAgent.toLowerCase();
	NativeBridge.prototype.QuickVersion = {
		
		isWeixin: u.indexOf("micromessenger") != -1,
		//是否为安卓端
		isApp: u.indexOf("android_medical_community") != -1 || u.indexOf("android_health_hainan") != -1,
		//是否为iOS段
		isIOSApp: u.indexOf("ios_medical_community") != -1 || u.indexOf("ios_health_hainan") != -1,
	};

	//回调方法
	NativeBridge.prototype.getAppIndex = function (name) {
		let thisIndex = this.indexCount++;
		let numString = name + thisIndex.toString();
		return numString;
	};
	//js调用原生
	NativeBridge.prototype.callApp = function (params) {
		if (this.QuickVersion.isIOSApp) {
			window.webkit.messageHandlers.nativePermission.postMessage(JSON.stringify(params));
		} else if (this.QuickVersion.isApp) {
			window.WDAndroid.nativePermission(JSON.stringify(params));
		} else {
			console.log("h5页面不支持", u);
		}
	};

	//原生回调js
	let regNumber = /\d+/;
	NativeBridge.prototype.appCall = function (callAppIndex, stringResult) {
		if (this.callAppMap[callAppIndex]) {
			//原生传过来的参数为JSON字符串
			let backData = JSON.parse(stringResult);
			//调用回调方法
			this.callAppMap[callAppIndex](backData);
			//回调方法，调用过后，移除该方法（除按钮方法、newMessage、跳转原生方法外）
			if (
				regNumber.test(callAppIndex) &&
				callAppIndex.indexOf("header") == -1 &&
				callAppIndex.indexOf("newMessage") == -1 &&
				callAppIndex.indexOf("forwardNative") == -1
			) {
				this.callAppMap[callAppIndex] = null;
				delete this.callAppMap[callAppIndex];
			} else {
				// console.log('不移除')
			}
		}
	};
	/**************** 注册交互方法 **********/

	NativeBridge.prototype.showMapRoute = function (data, callback) {
		let appIndex = this.getAppIndex("showMapRoute");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = {
			type: "showMapRoute",
			params: data,
			callBackMethod: appIndex
		};
		this.callApp(params);
	};

	NativeBridge.prototype.wxPay = function (data, callback) {
		let appIndex = this.getAppIndex("wxPay");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "wxPay", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};

	NativeBridge.prototype.qrCodeScan = function (data, callback) {
		let appIndex = this.getAppIndex("qrCodeScan");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "qrCodeScan", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.callPhone = function (data, callback) {
		let appIndex = this.getAppIndex("callPhone");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "phone", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.getVersion = function (callback) {
		let appIndex = this.getAppIndex("getVersion");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "version", callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.share = function (data, callback) {
		let appIndex = this.getAppIndex("share");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "share", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.getLocation = function (data, callback) {
		let appIndex = this.getAppIndex("getLocation");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "location", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.chooseImage = function (data, callback) {
		let appIndex = this.getAppIndex("chooseImage");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = {
			type: "photoLibrary",
			params: data,
			callBackMethod: appIndex
		};
		this.callApp(params);
	};
	NativeBridge.prototype.camera = function (data, callback) {
		let appIndex = this.getAppIndex("camera");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "camera", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.nativeStorage = function (data, callback) {
		let appIndex = this.getAppIndex("nativeStorage");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = {
			type: "nativeStorage",
			params: data,
			callBackMethod: appIndex
		};
		this.callApp(params);
	};
	NativeBridge.prototype.nativeVioce = function (callback) {
		let appIndex = this.getAppIndex("nativeVioce");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "voice", callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.nativeSearchVal = function (data) {
		let params = { type: "searchContent", params: data };
		this.callApp(params);
	};
	NativeBridge.prototype.authentication = function (data, callback) {
		let appIndex = this.getAppIndex("authentication");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = {
			type: "authentication",
			params: data,
			callBackMethod: appIndex
		};
		this.callApp(params);
	};
	NativeBridge.prototype.authType = function (callback) {
		let appIndex = this.getAppIndex("authType");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "authType", callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.rotate = function (data) {
		let params = { type: "rotate", params: data };
		this.callApp(params);
	};
	NativeBridge.prototype.login = function (data, callback) {
		let appIndex = this.getAppIndex("login");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "login", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.download = function (data) {
		let params = { type: "download", params: data };
		this.callApp(params);
	};
	NativeBridge.prototype.cacheSize = function (callback) {
		let appIndex = this.getAppIndex("cacheSize");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "cacheSize", callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.clearCache = function (callback) {
		let appIndex = this.getAppIndex("clearCache");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "clearCache", callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.newMessage = function (data, callback) {
		let appIndex = this.getAppIndex("newMessage");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "newMessage", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.video = function (data, callback) {
		let appIndex = this.getAppIndex("video");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "video", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};
	NativeBridge.prototype.bluetoothDevice = function (data, callback) {
		let appIndex = this.getAppIndex("bluetoothDevice");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = {
			type: "bluetoothDevice",
			params: data,
			callBackMethod: appIndex
		};
		this.callApp(params);
	};
	NativeBridge.prototype.refreshToken = function (data, callback) {
		let appIndex = this.getAppIndex("refreshToken");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = {
			type: "refreshToken",
			params: data,
			callBackMethod: appIndex
		};
		this.callApp(params);
	};
	NativeBridge.prototype.changeRole = function () {
		let params = {
			type: "changeRole",
			params: {}
		};
		this.callApp(params);
	};
	NativeBridge.prototype.logout = function () {
		let params = {
			type: "logout",
			params: {}
		};
		this.callApp(params);
	};
	NativeBridge.prototype.tabConfig = function (data) {
		let params = { type: "tabConfig", params: data };
		this.callApp(params);
	};
	NativeBridge.prototype.setHeader = function (data) {
		function deepCopy(obj) {
			let newobj = obj.constructor === Array ? [] : {};
			if (typeof obj !== "object") {
				return obj;
			} else {
				for (let i in obj) {
					if (typeof obj[i] === "object") {
						//判断对象的这条属性是否为对象
						newobj[i] = deepCopy(obj[i]); //若是对象进行嵌套调用
					} else {
						newobj[i] = obj[i];
					}
				}
			}
			return newobj; //返回深度克隆后的对象
		}
		let nativeData = deepCopy(data);
		if (data["right"] && data["right"].length > 0) {
			let rightLenth = data["right"].length;
			for (let i = 0, len = rightLenth; i < len; i++) {
				let appIndex = "header-right" + i;
				this.callAppMap[appIndex] = data["right"][i]["callBackMethod"];
				nativeData["right"][i]["callBackMethod"] = appIndex;
			}
		}

		if (data["left"] && data["left"].length > 0) {
			let leftLenth = data["left"].length;
			for (let j = 0, len = leftLenth; j < len; j++) {
				let appIndex = "header-left" + j;
				this.callAppMap[appIndex] = data["left"][j]["callBackMethod"];
				nativeData["left"][j]["callBackMethod"] = appIndex;
			}
		}
		if (data.titleView && data.titleView.searchMethods && data.titleView.searchMethods.editingdidbegin) {
			let appIndex1 = "editingdidbegin";
			this.callAppMap[appIndex1] = data.titleView.searchMethods.editingdidbegin;
			nativeData.titleView.searchMethods.editingdidbegin = appIndex1;
		}
		if (data.titleView && data.titleView.searchMethods && data.titleView.searchMethods.editingdidend) {
			let appIndex2 = "editingdidend";
			this.callAppMap[appIndex2] = data.titleView.searchMethods.editingdidend;
			nativeData.titleView.searchMethods.editingdidend = appIndex2;
		}
		if (data.titleView && data.titleView.searchMethods && data.titleView.searchMethods.editingchanged) {
			let appIndex3 = "editingchanged";
			this.callAppMap[appIndex3] = data.titleView.searchMethods.editingchanged;
			nativeData.titleView.searchMethods.editingchanged = appIndex3;
		}
		if (data.titleView && data.titleView.searchMethods && data.titleView.searchMethods.editingfinished) {
			let appIndex4 = "editingfinished";
			this.callAppMap[appIndex4] = data.titleView.searchMethods.editingfinished;
			nativeData.titleView.searchMethods.editingfinished = appIndex4;
		}
		if (data.titleView && data.titleView.callBackMethod) {
			let appIndex5 = "searchCallBackMethod";
			this.callAppMap[appIndex5] = data.titleView.callBackMethod;
			nativeData.titleView.callBackMethod = appIndex5;
		}
		if (this.QuickVersion.isIOSApp) {
			window.webkit.messageHandlers.setHeader.postMessage(JSON.stringify(nativeData));
		} else if (this.QuickVersion.isApp) {
			window.WDAndroid.setHeader(JSON.stringify(nativeData));
		}
	};

	NativeBridge.prototype.onVisible = function (onVisible) {
		if (this.QuickVersion.isIOSApp || this.QuickVersion.isApp) {
			this.callAppMap["onVisible"] = onVisible;
		}
	};
	NativeBridge.prototype.onInvisible = function (onInvisible) {
		if (this.QuickVersion.isIOSApp || this.QuickVersion.isApp) {
			this.callAppMap["onInvisible"] = onInvisible;
		}
	};
	NativeBridge.prototype.onDestory = function (onDestory) {
		if (this.QuickVersion.isIOSApp || this.QuickVersion.isApp) {
			this.callAppMap["onDestory"] = onDestory;
		}
	};

	NativeBridge.prototype.forwardNative = function (data, callback) {
		let appIndex = this.getAppIndex("forwardNative");
		if (callback) {
			this.callAppMap[appIndex] = callback;
		}
		let params = { type: "forwardNative", params: data, callBackMethod: appIndex };
		this.callApp(params);
	};

	NativeBridge.prototype.loginIM = function () {
		let params = { type: "loginIM", params: {} };
		this.callApp(params);
	};

	NativeBridge.prototype.toNative = function (router, nativeData, refreshUrl, animate, hasNavigation, float) {
		let params = {
			type: "Native",
			toPage: router,
			params: nativeData != undefined ? nativeData : {},
			refreshUrl: refreshUrl != undefined ? refreshUrl : "*",
			animate: animate == undefined ? "push" : animate,
			hasNavigation: hasNavigation || "true",
			float: float || "false"
		};
		if (this.QuickVersion.isIOSApp) {
			window.webkit.messageHandlers.forward.postMessage(JSON.stringify(params));
		} else if (this.QuickVersion.isApp) {
			window.WDAndroid.forward(JSON.stringify(params));
		}
	};
	NativeBridge.prototype.toBack = function (url, nativeData, refreshUrl, hasNavigation, float) {
		let params = {
			type: "H5",
			toPage: url || "",
			params: nativeData != undefined ? nativeData : {},
			refreshUrl: refreshUrl || "",
			hasNavigation: hasNavigation || "true",
			float: float || "false",
			animate: "pop"
		};
		if (this.QuickVersion.isIOSApp) {
			window.webkit.messageHandlers.forward.postMessage(JSON.stringify(params));
		} else if (this.QuickVersion.isApp) {
			window.WDAndroid.forward(JSON.stringify(params));
		}
	};
	NativeBridge.prototype.toPage = function (url, nativeData, hasNavigation, float) {
		let params = {
			type: "H5",
			toPage: url,
			params: nativeData != undefined ? nativeData : {},
			hasNavigation: hasNavigation || "true",
			float: float || "false",
			animate: "push"
		};
		if (this.QuickVersion.isIOSApp) {
			console.log("isIOSApp");
			window.webkit.messageHandlers.forward.postMessage(JSON.stringify(params));
		} else if (this.QuickVersion.isApp) {
			console.log("isApp");
			window.WDAndroid.forward(JSON.stringify(params));
		} else {
			console.log("H5");
		}
	};

	let nativeBridge = new NativeBridge();
	export default nativeBridge;

	// export const  thisNativeBridge;
// 	if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
// 		module.exports = thisNativeBridge;
// 	} else {
// 		window.NativeBridge = thisNativeBridge;
// 	}
// })(window);
